package com.mjlf.cloud.feignconsumer.controller;

import com.mjlf.cloud.feignconsumer.service.HelloService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @auther a123
 * @create 2018-02-27 10:48
 * @desc
 */
@RestController
public class HelloController {

    @Autowired
    private HelloService helloSerivce;

    @RequestMapping(value = "/feign-consumer", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "backMethod")
//    开启容错， 如果该出现错误， 立即执行backMethod方法，
//    注意， 此处的backMethod方法的参数必须和helloConsumer相同， 否则会提示找不到对应的方法
//    详细配置使用教程：https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#configuration
    /**
     * @HystrixCommand(commandProperties = {
    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
    },
    threadPoolProperties = {
    @HystrixProperty(name = "coreSize", value = "30"),
    @HystrixProperty(name = "maxQueueSize", value = "101"),
    @HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),
    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"),
    @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")
    })
     */
    public String helloConsumer(HttpServletRequest request, HttpServletResponse response){
        return helloSerivce.hello(request.getParameterMap());
    }

    public String backMethod(HttpServletRequest request, HttpServletResponse response){
        return "error";
    }
}
